package com.easyidea.its.domain.service.base.impl;

import java.io.Serializable;

import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;

import com.easyidea.its.domain.repository.base.BaseBeanRepository;
import com.easyidea.its.domain.service.base.BaseBeanService;

/**
 * 
 * @author hukn
 * 
 * @param <T>
 * @param <R>
 */
public abstract class BaseBeanServiceImpl<T, R extends BaseBeanRepository<T>> implements BaseBeanService<T, R> {

	public abstract R getBaseRepository();

	public BaseBeanServiceImpl() {

	}

	public boolean existsBean(Serializable id) {
		T bean = getBaseRepository().getBeanById(id);
		return bean != null;
	}

	public T getBeanById(Serializable id) {
		return getBaseRepository().getBeanById(id);
	}

	public void saveOrUpdateBean(T bean) {
		getBaseRepository().saveOrUpdateBean(bean);
	}

	protected Projection getDistinctProperties(String... properties) {
		ProjectionList list = Projections.projectionList();
		for (int i = 0; i < properties.length; ++i) {
			list.add(Projections.property(properties[i]), properties[i]);
		}
		Projection projection = Projections.distinct(list);
		return projection;
	}

	public boolean isRepeated(String propertyName, String propertyValue, String idpropertyName, Serializable idValue) {
		return getBaseRepository().isRepeated(propertyName, propertyValue, idpropertyName, idValue);
	}

	public void removeBean(Serializable id) {
		getBaseRepository().removeBean(id);
	}

}
